// userPageB/pages/userStore/storeExchangeAssets/storeExchangeAssets.js
const app = getApp()
const WXAPI = require("../../../../api/api")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    nvabarData: {
      showCapsule: 1, // 是否显示左上角图标   1表示显示    0表示不显示
      title: '选择您想要的资产', // 导航栏 中间的标题
      textColor: '#0D0400', // 标题颜色
      bgColor: '#fff', // 导航栏背景颜色
      isloading: false,
      isColor: true,
     
    },
    imgHttp: app.globalData.imgHttp,
    assetId: '',
    merchantId: '',
    assetsList: [],
    ExchangeData:{},
    selAssets:[],//选中的数组
    isDetail: false,//明细弹窗
    obtainData:{},//获得
    obtainNum: 0,//底部获得资产
    consumptionData:0,//消耗
    consumptionNum: 0,//底部消耗资产
    showDetailBtn: false,//明细按钮是否显示
   
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // var assetId = '1383331097373020162'
    var assetId = options.assetId
    var worth = options.worth
    var assetName = options.name
    var obtainData = this.data.obtainData
    obtainData.worth = worth
    obtainData.assetName = assetName
    this.setData({
      assetId: assetId,
      obtainData:obtainData,
    })
    this.getList()
  },

  //列表
  getList(){
    var assetId = this.data.assetId
    var data = {
      assetId: assetId,
    }
    WXAPI.storesExchange(data,(res=>{
      console.log('列表',res)
      const {result} = res
      const ExchangeData = result
      ExchangeData.assetsNum = 0 
      this.setData({
        ExchangeData: ExchangeData,
      })
      console.log('ExchangeData',this.data.ExchangeData)
    }))
  },
  //减
  handleDel(e){
    var eid = e.currentTarget.dataset.eid
    var assetsNum = e.currentTarget.dataset.num
    var ExchangeData = this.data.ExchangeData
    if(ExchangeData.assetsNum <= 0){
      ExchangeData.assetsNum = 0
    }else{
      ExchangeData.assetsNum --
    }

    this.setData({
      ExchangeData: ExchangeData,
      showDetailBtn: true,
    })
    this.realTime()
  },
  //输入
  handleInp(e){
    var val = parseInt(e.detail.value)
    console.log('val',val)
    var eid = e.currentTarget.dataset.eid
    var assetsNum = e.currentTarget.dataset.num
    var ExchangeData = this.data.ExchangeData
    if(val >=  ExchangeData.usable){
      ExchangeData.assetsNum =ExchangeData.usable
    }else{
      ExchangeData.assetsNum =  val
    }
    
    this.setData({
      ExchangeData: ExchangeData,
      showDetailBtn: true,
    })
    this.realTime()
  },
  handleblur(){
    
  },
  //加
  handleAdd(e){
    var assetsNum = e.currentTarget.dataset.num
    var ExchangeData = this.data.ExchangeData
    if(ExchangeData.assetsNum >= ExchangeData.usable){
      ExchangeData.assetsNum == ExchangeData.usable
    }else{
      ExchangeData.assetsNum ++
    }
    
    this.setData({
      ExchangeData: ExchangeData,
      showDetailBtn: true,
    })
    console.log('ExchangeData',this.data.ExchangeData)
    this.realTime()
  },
  //实时计算
  realTime(){
    var ExchangeData = this.data.ExchangeData
    var obtainData = this.data.obtainData
    var consumptionNum = 0
    var obtainNum =  0
    if(ExchangeData.assetsNum > 0){
      consumptionNum = consumptionNum + ExchangeData.assetsNum * parseInt(ExchangeData.exchangeRatio)
      obtainNum = obtainNum + parseInt(ExchangeData.assetsNum)
    }else{
      obtainNum = 0
    }
    obtainData.obtainNum = obtainNum
    this.setData({
      obtainNum: obtainNum,
      consumptionNum: consumptionNum,
      obtainData: obtainData,
    })
  },
  //明细
  handleDetail(){
    var ExchangeData = this.data.ExchangeData
    var selAssets = this.data.selAssets
    var consumptionData= 0
    var assetsObj = {}
    assetsObj.linkageId = ExchangeData.walletId
    assetsObj.linkageAssetId = ExchangeData.exchangeAssetId
    assetsObj.assetName = ExchangeData.assetName
    assetsObj.number = ExchangeData.assetsNum * parseInt(ExchangeData.exchangeRatio)
    assetsObj.worth = ExchangeData.worth
    selAssets.push(assetsObj)
    consumptionData = consumptionData + ExchangeData.worth*ExchangeData.assetsNum * parseInt(ExchangeData.exchangeRatio)
    this.setData({
      isDetail: !this.data.isDetail,
      selAssets: selAssets,
      consumptionData: consumptionData,
    })
    console.log('this.data.isDetail',this.data.isDetail)
    if(!this.data.isDetail){
      this.setData({
        selAssets: [],
        consumptionData: 0,
      })
    }
  },
  //立即兑换
  subExchange(){
    var assetsList = this.data.assetsList
    var selAssets = []
   
    // payexchangeAsset
    var data = {
      walletId: this.data.ExchangeData.walletId,
      exchangeAssetId: this.data.ExchangeData.exchangeAssetId,
      exchangeCount: this.data.ExchangeData.assetsNum,
    }
    console.log('data',data)
    WXAPI.storesExchangeWallet(data,(res)=>{
      console.log('立即兑换',res)
      const {assetName,exchangeAssetList,obtainCount} = res.result
      var fls = JSON.stringify(exchangeAssetList)
      console.log('fls',fls)
      if(res.code == 200){
        wx.showToast({
          title: '兑换成功',
        })
        setTimeout(() => {
          wx.navigateTo({
            url: '/userPageB/pages/userStore/exchangeSuccess/exchangeSuccess?assetName='+assetName+'&obtainCount='+obtainCount+'&fls='+fls,
          })
        }, 1500);
       
      }
    })
  },
 

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  
})